<?php
use PFT\Tool\Tools;
use PFT\Db;
use PFT\AllDis;

header('Access-Control-Allow-Origin:*');

include '../../class/Tools.class.php';
include '../../class/AllDis.class.php';
include '/var/www/html/new/d/module/common/Db.class.php';
// include '/var/www/html/new/d/module/common/Db.class.php';

$db = array(
	'host' => 'www.losemind.com',
	'username' => 'wengbin',
	'pwd' => '111111',
	'dbname' => 'test',
	'port' => 3306
);
Db::Conf($db);
Db::ConnectRef();

$req = Tools::deepRemoveXss($_REQUEST);

//TODO；判断是否供应商身份
// if (!isset($_SESSION['memberID'])) {
// 	ajaxReturn('noLogin');
// }
$member_id = $_SESSION['memberID'];
$member_id = 3385;

switch ($req['action']) {
	//是否开启全民分销
	case 'get_status':
		$config = AllDis::getConfig($member_id);
		if ($config && $config['status'] == 1) {
			ajaxReturn('getStatus', 1);
		} else {
			ajaxReturn('getStatus', 0);
		}
		break;

	case 'open_all_dis':
		//TODO:是否绑定微信号
		$result = AllDis::openAllDis($member_id);
		ajaxReturn($result ? 'success' : 'fail');
		break;

	//获取全民分销的相关配置
	case 'get_config':
		$config = AllDis::getConfig($member_id);
		if ($config['status'] == 0) {
			ajaxReturn('limited');
		}
		$config = json_decode($config['config'], true);
		ajaxReturn('configInfo', null, $config);
		break;

	//保存全民分销的相关配置
	case 'save_config':
		if (!isset($req['develop_per'], $req['recommend_per'], $req['return_per'])) {
			ajaxReturn('paramError');
		}
		$config = array(
			'develop_per' => intval($req['develop_per']),
			'recommend_per' => intval($req['recommend_per']),
			'return_per' => intval($req['return_per'])
		);
		extract($config);
		//平台佣金暂定为0
		
		if (($develop_per + $recommend_per + $return_per) > 100) {
			ajaxReturn('configError');
		} 

		$config['platform_per'] = 0;
		$config['type'] = intval($req['type']);
		$result = AllDis::saveConfig($member_id, $config);

		ajaxReturn($result ? 'success' : 'fail');
		break;

	//获取用户的微信授权
	case 'authorization':
		ECHO $redirect = $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];

		var_dump($_SERVER);DIE;
		$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd72be21f7455640d&redirect_uri={$redirect}&response_type=code&scope=snsapi_base&connect_redirect=1#wechat_redirect';

	//获取下属的分销人员信息
	case 'get_distributors':

		break;

	default:
		@header('HTTP/1.1 400 Bad Request');
}

function ajaxReturn($code, $msg = null, $data = null) {
	switch ($code) {
		case 'noLogin':
			$status = -1;
			$msg = '请先登录';
			break;
		case 'limited':
			$status = 0;
			$msg = '未开通全民分销';
			break;
		case 'paramError':
			$status = 0;
			$msg = '参数错误';
			break;
		case 'configError':
			$status = 0;
			$msg = '配置错误';
			break;
		case 'configInfo':
			$status = 1;
			break;
		case 'getStatus':
			$status = 1;
			$msg = $msg;
			break;
		case 'success':
			$status = 1;
			$msg = '操作成功';
			break;
		case 'fail':
			$status = 0;
			$msg = '操作失败';
			break;
		default:
			exit();
	}
	$return = array(
		'status' => $status,
		'code' => $code ,
		'msg' => $msg,
		'data' => $data
	);
	// $return = array_filter($return);
	// $return['status'] = $status;
	exit(json_encode($return));
}

// ajax
